﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class OrderFileController : MyControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        OrderFileManager manager;
        PicFetcher fetcher;
        public OrderFileController(IHostingEnvironment hostingEnvironment) : base()
        {
            this.manager = new OrderFileManager();
            this.fetcher = new PicFetcher();
            _hostingEnvironment = hostingEnvironment;
        }
        [HttpPost]
        [Authorize]
        public AsmObject<PicModel> Set(dynamic data)
        {
            string ownerid = data.order;
            string file = data.data;
            var result = manager.CreateFileForOrder(_hostingEnvironment.WebRootPath,file, int.Parse(ownerid));
            DBHelper.Commit( true);
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmObject<bool> Remove(int id)
        {
            var result = manager.Delete(id);
            DBHelper.Commit(result);
            return new AsmObject<bool>() { Data = result, Message = "", Result = result ? AmsCode.Succ : AmsCode.InnError };
        }
        [HttpGet]
        [Authorize]
        public AsmArray<PicModel> ListByOrder(int orderid)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYORDER:[RQS|{1}]{0}", orderid, RequestIp));
            int cnt = 0;
            var list = fetcher.ListByOwner(1, orderid);
            DBHelper.Commit(true);
            var result = new AsmArray<PicModel>() { Data = list, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYORDER:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}